home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 2000 July / macformat-092.iso / Fireworks 3 / Settings / Batch Code / BatchTemplate.jst < prev   
Encoding:
Text File  |  1999-11-19  |  10.5 KB  |  325 lines

  1.  
  2. // Macromedia Fireworks Batch Script Template
  3. // Copyright (c) 1998, 1999 Macromedia. All rights reserved.
  4.  
  5. var batchArray = [
  6. #BATCH_SETTINGS#
  7. ];
  8.  
  9.     // ----------------------------------------------------------
  10.  
  11.     if (this.fw == null) {
  12.         var msg = Errors.EBadJsVersion;
  13.         if (msg == null)
  14.             msg = "This script does not work in this version of Fireworks.";
  15.         alert(msg);
  16.         theDocList = null;
  17.     } else {
  18.         fw.checkFwJsVersion(0);
  19.         if (theDocList == null || theDocList.length == 0) {
  20.             theDocList = App.chooseScriptTargetDialog(App.getPref("MultiFileBatchTypes"));
  21.         }
  22.     }
  23.     
  24.     if (theDocList == null) {
  25.         // The user must have canceled the "select files" dialog.
  26.     } else if (theDocList.length == 0) {
  27.         // The user did something like "current files" when no files are open.
  28.         alert(Errors.ENoFilesSelected);
  29.     } else {
  30.         App.progressCountCurrent = 0;
  31.         App.progressCountTotal = theDocList.length;
  32.         for (var i = 0; i < theDocList.length; i++) {
  33.             App.progressCountCurrent = i + 1;
  34.             App.batchStatusString = "";
  35.             if (ProcessOneDocPath(theDocList[i]) == false)
  36.                 break;
  37.         }    
  38.     }
  39.  
  40.     // ----------------------------------------------------------
  41.  
  42.     function ProcessOneDocPath(docPathName)
  43.     {
  44.         var result = true;
  45.         var theDocWasOpen = false;
  46.         var theDoc = App.findOpenDocument(docPathName);
  47.  
  48.         if (theDoc == null) {
  49.             theDoc = App.openDocument(docPathName, false);
  50.             theDocWasOpen = false;
  51.         } else {
  52.             theDocWasOpen = true;
  53.         }
  54.  
  55.         if (ProcessOneDoc(theDoc) == false)
  56.             result = false;
  57.  
  58.         if (theDoc != null && theDocWasOpen == false)
  59.             theDoc.close(false);    // discard changes
  60.  
  61.         return result;
  62.     }
  63.  
  64.     // ----------------------------------------------------------
  65.  
  66.     function ProcessOneDoc(theDoc)
  67.     {
  68.  
  69.         // If sourceDocumentPath is null, the file is either a new document,
  70.         // was opened as untitled (e.g., via stationery), or is a nonnative
  71.         // file format (e.g., .psd). In these cases, get the revert file path.
  72.         // If *that* is null, abort.
  73.         var sourceDocumentPath = theDoc.filePathForSave;
  74.         if (sourceDocumentPath == null)
  75.             sourceDocumentPath = theDoc.filePathForRevert;
  76.         if (sourceDocumentPath == null) {
  77.             alert(Errors.EInternalError);
  78.             return false;
  79.         }
  80.         
  81.         App.batchStatusString = Files.getFilename(sourceDocumentPath);
  82.  
  83.         for (var i = 0; i < batchArray.length; i++) {
  84.             if (ProcessOneDocForOneBatch(theDoc, sourceDocumentPath, batchArray[i]) == false)
  85.                 return false;
  86.         }
  87.  
  88.         return true;
  89.     }
  90.     
  91.     // ----------------------------------------------------------
  92.  
  93.     function ProcessOneDocForOneBatch(theDoc, sourceDocumentPath, batch)
  94.     {
  95.         var backupPath = null;
  96.  
  97.         if (batch.doBackupFiles) {
  98.  
  99.             backupPath = GetBackupFile(sourceDocumentPath, batch.doIncrementalBackup);
  100.             if (backupPath == null) {
  101.                 alert(Errors.EFileNotFound);
  102.                 return false;
  103.             }
  104.  
  105.             var errorString = SafeMoveFileTo(sourceDocumentPath, backupPath);
  106.             if (errorString != null) {
  107.                 alert(errorString);
  108.                 return false;
  109.             }
  110.  
  111.             // Note that Files.swap(), and thus SafeMoveFileTo(), will change theDoc to
  112.             // refer to backupPath rather than sourceDocumentPath. We don't want this, 
  113.             // so we force the issue by setting it back to what we want. Note also that
  114.             // it is not necessarily the case that theDoc.filePathForSave == sourceDocumentPath
  115.             // (e.g., if the document was not originally a native Fireworks file), so we
  116.             // really only want to reset it if it changed.
  117.             if (theDoc.filePathForSave == backupPath) {
  118.                 theDoc.filePathForSave = sourceDocumentPath;
  119.             }
  120.         }
  121.  
  122.         if (batch.findAndReplaceParms != null) {
  123.             var theFinder = theDoc.makeFind(batch.findAndReplaceParms);        
  124.             var replacedAnything = theFinder.replaceAll();
  125.             if (theDoc.filePathForSave == null) {
  126.                 theDoc.filePathForSave = Document.makeGoodNativeFilePath(sourceDocumentPath);
  127.                 if (theDoc.filePathForSave == null) {
  128.                     // This should never happen, but check, just in case.
  129.                     alert(Errors.EInternalError);
  130.                     return false;
  131.                 }
  132.             }
  133.             if (replacedAnything) {
  134.                 // Save the changes.
  135.                 theDoc.save();
  136.             } else {
  137.                 // Don't save the file ... we didn't do anything.
  138.                 // But, if we are doing backups, make a copy of the original file
  139.                 // (now located in the Original Files folder) back in the original spot.
  140.                 if (batch.doBackupFiles && backupPath != null && sourceDocumentPath != null) {
  141.                     var errorString = SafeCopyFileTo(backupPath, sourceDocumentPath);
  142.                     if (errorString != null) {
  143.                         alert(errorString);
  144.                         return false;
  145.                     }
  146.                 }
  147.             }
  148.             if (theDoc.filePathForSave == null) {
  149.                 // This should never happen, but check, just in case.
  150.                 alert(Errors.EInternalError);
  151.                 return false;
  152.             }
  153.         }
  154.         
  155.         if (batch.exportOptions != null) {
  156.  
  157.             var curExportFormatOptions;
  158.             if (batch.exportOptions.useFormatOptionsFromEachFile) {
  159.                 curExportFormatOptions = theDoc.exportFormatOptions;
  160.             } else {
  161.                 curExportFormatOptions = Document.findExportFormatOptionsByName(batch.exportOptions.exportFormatOptions.name);
  162.                 if (curExportFormatOptions == null)
  163.                     curExportFormatOptions = batch.exportOptions.exportFormatOptions;
  164.             }
  165.             
  166.             // Copy the scaling/cropping info back over, since we may have
  167.             // gotten that info from the doc or the named settings,
  168.             // and want to override it here.
  169.             if (batch.exportOptions.exportFormatOptions != null) {
  170.                 curExportFormatOptions.applyScale = batch.exportOptions.exportFormatOptions.applyScale;
  171.                 curExportFormatOptions.useScale = batch.exportOptions.exportFormatOptions.useScale;
  172.                 curExportFormatOptions.percentScale = batch.exportOptions.exportFormatOptions.percentScale;
  173.                 curExportFormatOptions.xSize = batch.exportOptions.exportFormatOptions.xSize;
  174.                 curExportFormatOptions.ySize = batch.exportOptions.exportFormatOptions.ySize;
  175.                 curExportFormatOptions.cropTop = batch.exportOptions.exportFormatOptions.cropTop;
  176.                 curExportFormatOptions.cropLeft = batch.exportOptions.exportFormatOptions.cropLeft;
  177.                 curExportFormatOptions.cropBottom = batch.exportOptions.exportFormatOptions.cropBottom;
  178.                 curExportFormatOptions.cropRight = batch.exportOptions.exportFormatOptions.cropRight;
  179.                 curExportFormatOptions.crop = batch.exportOptions.exportFormatOptions.crop;
  180.             }
  181.                                 
  182.             // Always disable cropping.
  183.             curExportFormatOptions.crop = false;
  184.             
  185.             // If you want to actually modify the document, jam the settings back in here, like so:
  186.             //             theDoc.exportFormatOptions = curExportFormatOptions;
  187.             // We don't usually want to do this; instead, we pass the export settings
  188.             // as the (optional) second argument to exportTo(), which will leave the document
  189.             // unaffected.
  190.  
  191.             var theDir = Files.getDirectory(sourceDocumentPath);
  192.             var theName = Files.getFilename(sourceDocumentPath, true).toString();    // strip extension, if any, and ensure string-ness
  193.             var theExtension = Files.getExtension(sourceDocumentPath).toString();
  194.             if (batch.exportOptions.filenamePrefix != null)
  195.                 theName = batch.exportOptions.filenamePrefix + theName;
  196.             if (batch.exportOptions.filenameSuffix != null)
  197.                 theName = theName + batch.exportOptions.filenameSuffix;
  198.             // re-add the original extension, if any. (Note that exportTo() will replace this
  199.             // extension with the correct one for the export format, if necessary, but re-adding
  200.             // the extension allows us to handle filenames with multiple periods more cleanly.)
  201.             theName += theExtension;
  202.             
  203.             if (App.platform == "mac") {
  204.                 // Macintosh filenames are limited to 31 characters (including the extension)
  205.                 // which is easy to overflow by accident, and produces weird errors if we
  206.                 // try to use 'em. The exporter will typically append an extension to the
  207.                 // end, which may be up to 5 characters long (well, it actually could be longer
  208.                 // but rarely is). So we will constrain the base filename here to 26 characters
  209.                 // ( == 31 - 5) so that this problem is minimized.
  210.                 if (theName.length > 26) {
  211.                     theName = theName.substr(0, 26);
  212.                 }
  213.             }
  214.             
  215.             var exportPath = Files.makePathFromDirAndFile(theDir, theName);
  216.  
  217.             theDoc.exportTo(exportPath, curExportFormatOptions);
  218.         }
  219.         
  220.         return true;
  221.     }
  222.  
  223.     // ----------------------------------------------------------
  224.  
  225.     function UniquePathnameWithSameExtension(pathname)
  226.     {
  227.         if (Files.exists(pathname) == false) {
  228.             return pathname;    // already unique
  229.         }
  230.         
  231.         var filename = Files.getFilename(pathname).toString(); // make sure it's a string, not a number
  232.         var extension = "";
  233.         var curlength = filename.length;
  234.         for (var i = 1; i < curlength - 1; i++) {
  235.             if (filename.charAt(curlength - i) == ".") {
  236.                 extension = filename.substr(curlength - i);
  237.                 filename = filename.substr(0, curlength - i);
  238.                 break;
  239.             }
  240.         }
  241.  
  242.         var newpathname = pathname;
  243.         var newfilename = "";
  244.         for (var j = 1; j < 10000; j++) {
  245.             newfilename = filename + "-" + j + extension;
  246.             newpathname = Files.setFilename(pathname, newfilename);
  247.             if (Files.exists(newpathname) == false)
  248.                 return newpathname;
  249.         }
  250.         
  251.         // We should never get here.
  252.         return null;
  253.     }
  254.  
  255.     // ----------------------------------------------------------
  256.  
  257.     function GetBackupDirectory(pathname)
  258.     {
  259.         if (Files.exists(pathname) == false) {
  260.             return null;
  261.         }
  262.         
  263.         var dir = Files.getDirectory(pathname);
  264.         var dirName = App.getPref("OriginalFilesFolderName");
  265.         var bkupDir = Files.makePathFromDirAndFile(dir, dirName);
  266.  
  267.         if (Files.exists(bkupDir) == false)
  268.             Files.createDirectory(bkupDir);
  269.  
  270.         if (Files.exists(bkupDir) == false) {
  271.             return null;
  272.         }
  273.  
  274.         if (Files.isDirectory(bkupDir) == false) {
  275.             return null;
  276.         }
  277.             
  278.         return bkupDir;
  279.     }
  280.     
  281.     // ----------------------------------------------------------
  282.  
  283.     function GetBackupFile(pathname, doIncrementalBackup)
  284.     {
  285.         var bkupDir = GetBackupDirectory(pathname);
  286.         if (bkupDir == null)
  287.             return null;
  288.         var filename = Files.getFilename(pathname);
  289.         var backupFile = Files.makePathFromDirAndFile(bkupDir, filename);
  290.         if (doIncrementalBackup)
  291.             backupFile = UniquePathnameWithSameExtension(backupFile);
  292.         return backupFile;
  293.     }
  294.  
  295.     // ----------------------------------------------------------
  296.  
  297.     function SafeCopyFileTo(sourcePath, destPath)
  298.     {
  299.         if (Files.deleteFileIfExisting(destPath) == false)
  300.             return Files.getLastErrorString();
  301.  
  302.         if (Files.copy(sourcePath, destPath) == false)
  303.             return Files.getLastErrorString();
  304.         
  305.         return null;
  306.     }
  307.  
  308.     // ----------------------------------------------------------
  309.  
  310.     function SafeMoveFileTo(sourcePath, destPath)
  311.     {
  312.         if (Files.deleteFileIfExisting(destPath) == false)
  313.             return Files.getLastErrorString();
  314.  
  315.         // Note: if destPath exists, the two files are swapped; if destPath
  316.         // does not exist, sourcePath is moved to destPath. Either way,
  317.         // both source and dest must point to the same volume, or the call
  318.         // will fail.
  319.         if (Files.swap(sourcePath, destPath) == false)
  320.             return Files.getLastErrorString();
  321.         
  322.         return null;
  323.     }
  324.  
  325.